<template>
  <div>
    <!--查询参数-->
    <params
      ref="params"
      v-show="!modifyEntity.showModify"
      @handleRefresh="handleRefresh"
      :loading="loading"
    ></params>
    <!--表格页面-->
    <my-table
      ref="myTable"
      v-show="!modifyEntity.showModify"
      :dataSource="dataSource"
      :columns="columns"
      :loading="loading"
      @handleAdd="handleAdd"
      @removeSubmit="removeSubmit"
      @handleEdit="handleEdit"
      @handleDetail="handleDetail"
      @loadData="loadData"
    >
      <template slot="roleId" slot-scope="{ text }">
        <span v-if="text.record.roleId === '1'">管理员</span>
        <span v-else>业务员</span>
      </template>

      <template slot="isPush" slot-scope="{ text }">
        <a-tag v-if="text.record.isPush" color="#f50">开</a-tag>
        <a-tag v-if="!text.record.isPush" color="#2db7f5">关</a-tag>
      </template>

      <template slot="status" slot-scope="{ text }">
        <a-switch
          :checked="text.record.status" 
          @change="changeStatus(text.record)"
          un-checked-children="禁用"
          checked-children="启用"
        />
      </template>

      <template v-slot:buttons="{ item }">
        <a @click="handleResetPwd(item)">重置密码</a>
      </template>
    </my-table>

    <!--修改页面-->
    <modify
      v-if="modifyEntity.showModify"
      @handleRefresh="handleRefresh"
      :modifyEntity="modifyEntity"
    />
  </div>
</template>
<script>
import modify from "./modify";
import myTable from "_c/myTable/index";
import params from "./components/params";
import {
  sysAdminColumns,
  querySysAdmin,
  deleteSysAdmin,
  resetPwd,
  updateStatus,
} from "@/api/sysAdmin";

export default {
  name: "SysAdmin",
  components: { params, modify, myTable },
  data() {
    return {
      modifyEntity: {
        // 传递的主键
        id: "",
        // 页面标题
        title: "",
        // 页面操作类型
        operation: "",
        // 是否展示编辑页面
        showModify: false,
      },
      // 加载动画
      loading: false,
      // 绑定的数据源
      dataSource: [],
      // 表格绑定的列
      columns: sysAdminColumns,
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    //#region 启用、禁用
    changeStatus(item) {
      let that = this;
      updateStatus({ id: item.id, status: !item.status }).then((res) => {
        if (res.success) {
          that.$notification.success({ description: res.msg });
          that.handleRefresh();
        } else {
          that.$notification.error({ description: res.msg });
        }
      });
    },
    //#endregion

    handleResetPwd(item) {
      var that = this;
      this.$confirm({
        title: "确定重置" + item.nickName + "的密码吗？？",
        onOk() {
          resetPwd({ id: item.id })
            .then((res) => {
              if (res.statusCode == 200) {
                that.handleRefresh();
                that.$notification.success({ description: res.msg });
              } else {
                that.$notification.warning({ description: res.msg });
              }
            })
            .catch((error) => {});
        },
        onCancel() {},
      });
    },
    //#region 查询列表
    // 保存/修改后刷新表格
    handleRefresh() {
      this.modifyEntity.showModify = false;
      this.$refs.myTable.pagination.current = 1;
      this.loadData();
    },
    // 加载数据
    loadData() {
      this.loading = true;
      let queryParams = {
        ...this.$refs.params.params,
        current: this.$refs.myTable.pagination.current,
        pageSize: this.$refs.myTable.pagination.pageSize,
      };
      let that = this;
      querySysAdmin(queryParams)
        .then((res) => {
          let { data, totalCount } = res.data;
          that.dataSource = data;
          this.$refs.myTable.pagination.total = totalCount;
          that.loading = false;
        })
        .catch(() => {
          that.loading = false;
        });
    },
    //#endregion

    //#region 添加
    // 打开添加页面
    handleAdd() {
      this.modifyEntity = {
        title: "新增管理员",
        showModify: true,
        operation: this.operationFlag.Add,
      };
    },
    //#endregion

    //#region 删除
    // 删除
    removeSubmit(ids) {
      deleteSysAdmin(ids).then((res) => {
        if (res.success) {
          this.$notification.success({ description: res.msg });
          this.handleRefresh();
        } else {
          this.$notification.error({ description: res.msg });
        }
      });
    },
    //#endregion

    //#region 修改
    // 打开编辑页面
    handleEdit(item) {
      this.modifyEntity = {
        id: item.id,
        title: "修改管理员",
        showModify: true,
        operation: this.operationFlag.Edit,
      };
    },
    //#endregion

    //#region 详情
    // 打开详情页面
    handleDetail(item) {
      this.modifyEntity = {
        id: item.id,
        title: "管理员详情",
        showModify: true,
        operation: this.operationFlag.Detail,
      };
    },
    //#endregion
  },
};
</script>
